const config = require("./grunt.config.js")
const loadTasks = require('load-grunt-tasks')
module.exports = function (grunt) {
    grunt.initConfig({
        // 清除文件
        clean: {
            main: [config.path.dist]
        },
        // sass转换
        sass: {
            main: {
                files: [{
                    expand: true,
                    cwd: config.path.src,
                    src: [config.path.styles],
                    dest: config.path.dist,
                    base: config.path.src,
                    ext: '.css'
                }]
            }
        },
        // js转换任务
        babel: {
            options: {
                sourceMap: true,
                presets: ['@babel/preset-env']
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: config.path.src,
                    src: [config.path.scripts],
                    dest: config.path.dist,
                    base: config.path.src,
                    ext: '.js'
                }]
            }
        },
        // 图片压缩，字体拷贝
        imagemin: {
            dist: {
                files: [{
                    expand: true,
                    cwd: config.path.src,
                    src: ['**/*.{png,jpg,jpeg,gif,svg,eot,ttf,woff}'],
                    dest: config.path.dist,
                    base: config.path.src
                }]
            }
        },
        swig_precompile: {
            options: {
                active: '',
                locals: {},
                beautify: {
                    indent_size: 2
                }
            },
            dev: {
                options: {
                    force: true
                },
                expand: true,
                cwd: config.path.src,
                src: "*.tpl",
                dest: config.path.dist
            }
        },
        // 复制其他资源文件
        copy: {
            html: {
                files: [
                    {
                        expand: true,
                        cwd: config.path.public,
                        src: ['**'],
                        dest: config.path.dist
                    }
                ]
            }
        },

        connect: {
            options: {
                port: 9000,
                hostname: '*', //默认就是这个值，可配置为本机某个 IP，localhost 或域名
                livereload: 35729  //声明给 watch 监听的端口
            },

            server: {
                options: {
                    open: true, //自动打开网页 http://
                    base: [
                        config.path.dist,  //主目录
                        config.path.src,
                        config.path.public
                    ]
                }
            }
        },

        watch: {
            livereload: {
                options: {
                    livereload: '<%=connect.options.livereload%>'  //监听前面声明的端口  35729
                },

                files: [  //下面文件的改变就会实时刷新网页
                    'dist/**'
                ]
            }
        },

        concat: {
            options: {
                separator: ';',
                stripBanners: true // 去掉代码中的注释块
            },
            js: {
                src: [
                    "./node_modules/jquery/dist/jquery.js",
                    "./node_modules/popper.js/dist/umd/popper.js",
                    "./node_modules/bootstrap/dist/js/bootstrap.js",
                ], // 需要构建的js路径  
                dest: "dist/assets/scripts/vendor.js" // 构建后js路径 
            },
            css: {
                src: [
                    "./node_modules/bootstrap/dist/css/bootstrap.css"
                ],
                dest: "dist/assets/styles/vendor.css"
            }
        },
        // 处理 html 文件中的构建注释
        useref: {
            html: 'dist/*.html',
            temp: config.path.dist
        }
    })

    loadTasks(grunt)

    // 解析html中文件引用
    grunt.registerTask('ref', ['concat', 'useref'])
    grunt.registerTask("compile", ["clean", "sass", "babel", "swig_precompile", "ref"])
    // 构建工作
    grunt.registerTask("build", ["compile", "imagemin", "copy"])
    // 开发工作
    grunt.registerTask('serve', ["build", 'connect:server', 'watch'])

}